<?php
class Application_Form_User extends Zend_Form
{
	protected $_type;
	public function __construct($type=null){		
		$this->_type = $type;
		parent::__construct();
	}
	
    public function init()
    {
        $this->setMethod('post');
                
        $userEl = new Zend_Form_Element_Text('name');
        $userEl->setLabel('Enter username')
        		->setOptions(array('size' => 25))
        		->setRequired(true)
        		->addFilter('StripTags')
        		->addFilter('StringTrim')
        		->addValidator('Alnum')		
        		->addValidator('Db_NoRecordExists', false, array('table' => 'user', 'field' => 'name'));       	
        $userEl->getValidator('Db_NoRecordExists')->setMessage('Username already exists', Zend_Validate_Db_NoRecordExists::ERROR_RECORD_FOUND);		
        
        $emailEl = new Zend_Form_Element_Text('email');
        $emailEl->setLabel('Enter your email address')
        		->setOptions(array('size' => 25))
        		->setRequired(true)
        		->addFilter('StripTags')
        		->addFilter('StringTrim')
        		->addValidator('EmailAddress')
        		->addValidator('Db_NoRecordExists', true, array('table' => 'user', 'field' => 'email'));       	
        $emailEl->getValidator('Db_NoRecordExists')->setMessage('Email already exists', Zend_Validate_Db_NoRecordExists::ERROR_RECORD_FOUND);
        		
        $passEl = new Zend_Form_Element_Password('password');
        $passEl->setLabel('Enter password')
        		->setOptions(array('size' => 25))
        		->setRequired(true)
        		->addFilter('StringTrim')
        		->addPrefixPath('App_Validate', 'App/Validate/', 'validate')
        		->addValidator('ConfirmPass');
        		
        $passConfEl = new Zend_Form_Element_Password('passwordConf');
        $passConfEl->setLabel('Confirm password')
        		->setOptions(array('size' => 25))
        		->setRequired(true)
        		->addFilter('StringTrim');
        		
        $statusEl = new Zend_Form_Element_Select('status');
	    $statusEl->setLabel('User status')
	        	->addMultiOption('user', 'user')
	        	->addMultiOption('admin', 'admin')
	        	->setRequired(true)
	        	->addFilter('StripTags')
	        	->addFilter('StringTrim');
        		
        $submitEl = new Zend_Form_Element_Submit('submit');
        $submitEl->setLabel('Add');
        
        if($this->_type == 'edit'){
        	$passEl->setRequired(false);
        	$passConfEl->setRequired(false);        	
        	$submitEl->setLabel('Edit');
        }

        $this->addElements(array($userEl, $emailEl, $passEl, $passConfEl, $statusEl, $submitEl));
    }
    public  function excludeFrom($element, $validator, $field, $value){
    	$this->$element->getValidator($validator)->setExclude(array('field'=>$field, 'value'=> $value));
    }
}